package com.company.hive.monitor;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 告警消息对象
 */
public class AlertMessage {
    
    private String alertType;
    private AlertLevel level;
    private String database;
    private String table;
    private String user;
    private Map<String, String> details;
    private long timestamp;
    
    private AlertMessage() {
        this.details = new HashMap<>();
        this.timestamp = System.currentTimeMillis();
    }
    
    public static Builder builder() {
        return new Builder();
    }
    
    public String getAlertType() {
        return alertType;
    }
    
    public AlertLevel getLevel() {
        return level;
    }
    
    public String getDatabase() {
        return database;
    }
    
    public String getTable() {
        return table;
    }
    
    public String getUser() {
        return user;
    }
    
    public Map<String, String> getDetails() {
        return details;
    }
    
    public long getTimestamp() {
        return timestamp;
    }
    
    public String getFormattedTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(new Date(timestamp));
    }
    
    /**
     * Builder模式构建告警消息
     */
    public static class Builder {
        private AlertMessage message;
        
        private Builder() {
            this.message = new AlertMessage();
        }
        
        public Builder alertType(String alertType) {
            message.alertType = alertType;
            return this;
        }
        
        public Builder level(AlertLevel level) {
            message.level = level;
            return this;
        }
        
        public Builder database(String database) {
            message.database = database;
            return this;
        }
        
        public Builder table(String table) {
            message.table = table;
            return this;
        }
        
        public Builder user(String user) {
            message.user = user;
            return this;
        }
        
        public Builder detail(String key, String value) {
            message.details.put(key, value);
            return this;
        }
        
        public Builder details(Map<String, String> details) {
            message.details.putAll(details);
            return this;
        }
        
        public AlertMessage build() {
            return message;
        }
    }
    
    @Override
    public String toString() {
        return "AlertMessage{" +
                "alertType='" + alertType + '\'' +
                ", level=" + level +
                ", database='" + database + '\'' +
                ", table='" + table + '\'' +
                ", user='" + user + '\'' +
                ", timestamp=" + getFormattedTime() +
                '}';
    }
}

